<?php
App::uses('AppController', 'Controller');
/**
 * Contacts Controller
 *
 * @property Contact $Contact
 */
class ContactsController extends AppController {

    /**
     * index method
     *
     * @return void
     */
   public function index() {
    $this->Contact->recursive = 0;
    $this->set('contacts', $this->paginate());
}
//Search Function for the controller
    public $name = 'Contacts';

        public function isAuthorized($user){
        if(in_array($this->action, array('delete','edit','add'))){
            if ($user['permission']!=1){

                return false;
            }

        }
        return true;

}


    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Contact->exists($id)) {
            throw new NotFoundException(__('Invalid contact'));
        }
        $options = array('conditions' => array('Contact.' . $this->Contact->primaryKey => $id));
        $this->set('contact', $this->Contact->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Contact->create();
            if ($this->Contact->save($this->request->data)) {
                $this->Session->setFlash(__('The contact has been saved'), 'default', array('class' => 'success') );
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The contact could not be saved. Please, try again.'));
            }
        }
        $mailLists = $this->Contact->MailList->find('list');
        $this->set(compact('mailLists'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->Contact->exists($id)) {
            throw new NotFoundException(__('Invalid contact'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Contact->save($this->request->data)) {
               $this->Session->setFlash(__('The edit has been saved'), 'default', array('class' => 'success') );
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The contact could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Contact.' . $this->Contact->primaryKey => $id));
            $this->request->data = $this->Contact->find('first', $options);
        }
        $mailLists = $this->Contact->MailList->find('list');
        $this->set(compact('mailLists'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Contact->id = $id;
        if (!$this->Contact->exists()) {
            throw new NotFoundException(__('Invalid contact'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Contact->delete()) {
            $this->Session->setFlash(__('Contact deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Contact was not deleted'));
        $this->redirect(array('action' => 'index'));
    }
    function upload() {
        if (empty($this->data)) {
            // $this->set('tags', $this->Post->Tag->generateList(null,null,null,null,"{n}.Tag.tag"));
            // $this->set('selectedTags', null);
            // $this->render();
        } else {
            // $this->cleanUpFields();

            // upload the file to the server
            $tableName = $this->data['dbtable'];

            // echo $tableName;

            $fileOK = $this->uploadFiles('files', $this->data['csv']);

            // print out data
            // $this->pa($this->data);

            echo $this->pa($fileOK);

            // echo $fileOK['urls'][0];

            $row = 1;
            if (($handle = fopen($fileOK['urls'][0], "r")) !== FALSE) {
                while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
                    $num = count($data);
                    // echo $this->pa($data);
                    // echo $row;
                    if($row != 1 ) {
                        $this->Contact->create();
                        $this->request->data['Contact']['status'] = $data[0];
                        $this->request->data['Contact']['first_name'] = $data[1];
                        $this->request->data['Contact']['middle_name'] = $data[2];
                        $this->request->data['Contact']['last_name'] = $data[3];
                        $this->request->data['Contact']['position'] = $data[4];
                        $this->request->data['Contact']['email'] = $data[5];
                        $this->request->data['Contact']['mobile'] = $data[6];
                        $this->request->data['Contact']['home'] = $data[7];
                        $this->request->data['Contact']['office'] = $data[8];
                        $this->request->data['Contact']['address'] = $data[9];
                        $this->request->data['Contact']['notes'] = $data[10];
                        $this->request->data['Contact']['list'] = $data[11];
                        $this->request->data['Contact']['created_date'] = $data[12];
                        $this->request->data['Contact']['modified_date'] = $data[13];
                        $this->request->data['Contact']['dept_of_cont'] = $data[14];
                        if($this->Contact->save($this->request->data)) {
                            // echo "SUCCESSSSS!";
                             // echo $this->pa($data);
                        } else {
                            // echo "ERRORRRRRRRRRRR!" ;
                            // echo $this->pa($data);
                        }


                    }
                    $row++;

                }
                fclose($handle);
            }


            $this->redirect(array('action' => 'result'));

            // $this->pa($this->data['xls']);

            // $this->pa($this->data['xls']['type']);

        }
    }
    function result() {

    }

}

